let data = {
    pagenum: 1,
    pagesize: 3
}

function randerArticle() {
    $.ajax({
        url: '/my/article/list',
        data: data,
        success: function (res) {
            if (res.status === 0) {
                showPage(res.total)
                let str = ''
                res.data.forEach(item => {
                str += `
                <tr>
                <td>${item.title}</td>
                <td>${item.cate_name}</td>
                <td>${item.pub_date}</td>
                <td>${item.state}</td>
                <td>
                    <a href="./edit.html?id=${item.id}"  target="ifm" class="layui-btn layui-btn-xs edit">编辑</a>
                    <button type="button" data-id="${item.id}" class="layui-btn layui-btn-xs layui-btn-danger del">删除</button>
                </td>
            </tr>
                
                `
            })
            $('tbody').html(str)
            }
         }
        
    })
}

randerArticle()
 //----------------------------------------分页------------------------
function showPage(a) {
    let laypage = layui.laypage;
  
    //执行一个laypage实例
    laypage.render({
        elem: 'page',
        count: a, //数据总数，从服务端得到
        limit: data.pagesize,
        curr:data.pagenum,
        layout: ['limit', 'prev', 'page', 'next', 'skip','count'],
        limits: [2, 3, 5, 10],
        theme:'#85f',
        jump: function(obj, first){
            //obj包含了当前分页的所有参数，比如：
            //首次不执行
            if(!first){
              //do something
             data.pagenum= obj.curr  //得到当前页，以便向服务端请求对应页的数据。
                data.pagesize = obj.limit //得到每页显示的条数
                randerArticle()
            }
          }
    });
}
//------------------------------筛选-----------------------------------

$.ajax({
    url: '/my/category/list',
    success: function (res) {
        let str = ''
        res.data.forEach(item => {
            str += `
            <option value="${item.id}">${item.name}</option>
            `
        })
        $('#category').append(str)
        let form = layui.form
        form.render()
     }
})

$('form').on('submit', function (e) {
    e.preventDefault()
    state = $('#state').val()
    cate_id = $('#category').val()
    if (state) {
        data.state=state
    } else {
        delete data.state
    }
    if (cate_id) {
        data.cate_id=cate_id
    } else {
        delete data.cate_id
    }
    data.pagenum=1
    randerArticle()
 })

 //--------------------------------------删除文章---------------------------------------

$('body').on('click', '.del',function () {
    let id = $(this).data('id')   
    $.ajax({
        url: '/my/article/delete/' + id,
        success: function (res) {
            if (res.status === 0) {
                layer.confirm('您确定要删除吗?', function(index){
                    //do something
                    layer.msg(res.message)
                    randerArticle()
                    layer.close(index);
                  });                      
            }
          }
    })
 })